﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TeamOPSchoolCup.Domain.POCO;
using TeamOPSchoolCup.Domain.DAL;
using TeamOPSchoolCup.Domain.Services;
using TeamOPSchoolCup.Domain.POCO.Members;
using System.Web.Security;
using TeamOPSchoolCup.Web.Models;
using System.Text;
namespace TeamOPSchoolCup.Web.Controllers
{
  [Authorize]
  public class MemberController : Controller
  {
    public ActionResult Profile(int memberId)
    {
      return View();
    }

    public ActionResult Settings(int memId)
    {
      Member member = MemberService.GetMember(memId);

      ViewBag.Member = member;
      return View(member);
    }

    [HttpPost]
    public ActionResult Settings(Member member)
    {
      if (ModelState.IsValid)
      {
        if (MemberService.ChangeLanguage(member, member.Language))
        {
          return RedirectToAction("Index");
        }
        else
        {
          return RedirectToAction("Settings");
        }
      }

      return View(member);
    }

    //For testing purposes
    public string DeleteAllUsers()
    {
      foreach (MembershipUser user in Membership.GetAllUsers())
      {
        Membership.DeleteUser(user.UserName);
      }
      return "All users were successfully deleted";
    }


    public ActionResult AddStudent()
    {
      return View();
    }

    [HttpPost]
    public ActionResult AddStudent(Student student)
    {
      if (ModelState.IsValid)
      {
        string password = RandomString(6, 12);
        MembershipCreateStatus createStatus;
        MembershipUser user = Membership.CreateUser(student.Member.Email, password, student.Member.Email, null, null, true, null, out createStatus);

        if (createStatus == MembershipCreateStatus.Success)
        {
          student.Member.UserId = user.ProviderUserKey.ToString();

          try
          {
            if (MemberService.AddStudent(student))
            {
              if (Roles.RoleExists("InActive")) Roles.AddUserToRole(user.UserName, "InActive");
              //TODO: send mail to student --Thomas
              ViewBag.Password = password;

              return View();
            }
            else
            {
              Membership.DeleteUser(user.UserName, true);
              return RedirectToAction("AddStudent");
            }
          }
          catch (Exception) {
            Membership.DeleteUser(user.UserName);
          }
        }
        else
        {
          ModelState.AddModelError("", createStatus.ToString());
        }
      }

      return View();
    }

    // [minSize, maxSize]
    private string RandomString(int minSize, int maxSize)
    {
      Random random = new Random((int)DateTime.Now.Ticks);
      StringBuilder builder = new StringBuilder();

      for (int i = 0; i < random.Next(minSize, maxSize + 1); i++)
      {
        char c;

        if (random.NextDouble() < 0.7)
        {
          c = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + (random.NextDouble() >= 0.5 ? 65 : 97))));
        }
        else //Add numeric char
        {
          c = Convert.ToChar(Convert.ToInt32(Math.Floor(10 * random.NextDouble() + 48)));
        }
        builder.Append(c);
      }

      return builder.ToString();
    }



    /* All have GET and POST
     * Update
     * Delete
     * ChangeName
     * ChangeEmail
     * ChangePassword
     * ChangeLanguage
     * Settings
     * Dashboard(memberId -> studentId of coachId)
     */
    // GET: /Account/ChangePassword
  }
}
